package com.dundun.common.utils;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回数据
 * @author wq
 */
public class Ajax extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public Ajax() {
        put("code", 0);
        put("msg","ok");
        put("success", true);
    }

    public static Ajax error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常");
    }

    public static Ajax error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static Ajax error(int code, String msg) {
        Ajax ajax = new Ajax();
        ajax.put("code", code);
        ajax.put("msg", msg);
        ajax.put("success",false);
        return ajax;
    }

    public static Ajax success(String msg) {
        Ajax ajax = new Ajax();
        ajax.put("msg", msg);
        ajax.put("success",true);
        return ajax;
    }

    public static Ajax success(Map<String, Object> map) {
        Ajax ajax = new Ajax();
        ajax.putAll(map);
        ajax.put("success",true);
        return ajax;
    }

    public static Ajax success() {
        return new Ajax();
    }

    @Override
    public Ajax put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Integer getCode() {
        return (Integer) this.get("code");
    }
}
